<template>
  <div class="user-activity">
    <div style="padding: 0.625rem">
      <el-table
        v-loading="loading"
        :data="pageData"
        fit
        style="width: 100%"
        @sort-change="handleSortChange"
      >
        <el-table-column
          label="ID"
          prop="id"
          sortable="custom"
          align="center"
          width="80"
        >
          <template #default="scope">
            <span>{{ scope.row.id }}</span>
          </template>
        </el-table-column>
        <el-table-column label="用户名">
          <template #default="scope">
            <span>{{ scope.row.username }}</span>
          </template>
        </el-table-column>
        <el-table-column label="状态">
          <template #default="scope">
            <el-tooltip
              v-if="scope.row.status == 'error'"
              class="item"
              effect="dark"
              :content="scope.row.error_message"
              placement="top"
            >
              <el-tag type="danger" effect="dark"> 失败 </el-tag>
            </el-tooltip>
            <el-tag
              v-if="scope.row.status == 'success'"
              :type="scope.row.status"
              effect="dark"
            >
              成功
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="登录IP">
          <template #default="scope">
            <span>{{ scope.row.login_ip }}</span>
          </template>
        </el-table-column>
        <el-table-column label="IP归属">
          <template #default="scope">
            <span>{{ scope.row.ip_address }}</span>
          </template>
        </el-table-column>
        <el-table-column label="客户浏览器">
          <template #default="scope">
            <span>{{ scope.row.browser_type }}</span>
          </template>
        </el-table-column>
        <el-table-column label="系统" class-name="status-col">
          <template #default="scope">
            <span>{{ scope.row.system_type }}</span>
          </template>
        </el-table-column>
        <el-table-column label="登录时间">
          <template #default="scope">
            <span>{{ scope.row.created_at }}</span>
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-if="total > 0"
        v-model:total="total"
        v-model:page="queryParams.p"
        v-model:limit="queryParams.limit"
        @pagination="handleQuery"
      />
    </div>
  </div>
</template>

<script setup lang="ts">
import { loginLogLst } from "@/api/user";
import { loginLog, loginLogQuery } from "@/api/loginLog/types";
const loading = ref(false); //  加载状态
const queryParams = reactive<loginLogQuery>({
  p: 1,
  limit: 10,
});
const total = ref(0); // 数据总数
const pageData = ref<loginLog[]>(); // 用户分页数据
function handleQuery() {
  loading.value = true;
  loginLogLst(queryParams)
    .then(({ data }) => {
      pageData.value = data.items;
      total.value = data.total;
    })
    .finally(() => {
      loading.value = false;
    });
}
/** 排序 */
function handleSortChange(data: any) {
  const { prop, order } = data;
  if (prop === "id") {
    if (order === "ascending") {
      queryParams.sort = "+id";
    } else {
      queryParams.sort = "-id";
    }
    handleQuery();
  }
}
onMounted(() => {
  handleQuery();
});
</script>

<style lang="scss" scoped>
.user-activity {
  .user-block {
    .username,
    .description {
      display: block;
      margin-left: 50px;
      padding: 2px 0;
    }

    .username {
      font-size: 16px;
      color: #000;
    }

    :after {
      clear: both;
    }

    .img-circle {
      border-radius: 50%;
      width: 40px;
      height: 40px;
      float: left;
    }

    span {
      font-weight: 500;
      font-size: 12px;
    }
  }

  .post {
    font-size: 14px;
    border-bottom: 1px solid #d2d6de;
    margin-bottom: 15px;
    padding-bottom: 15px;
    color: #666;

    .image {
      width: 100%;
      height: 100%;
    }

    .user-images {
      padding-top: 20px;
    }
  }

  .list-inline {
    padding-left: 0;
    margin-left: -5px;
    list-style: none;

    li {
      display: inline-block;
      padding-right: 5px;
      padding-left: 5px;
      font-size: 13px;
    }

    .link-black {
      &:hover,
      &:focus {
        color: #999;
      }
    }
  }
}

.box-center {
  margin: 0 auto;
  display: table;
}

.text-muted {
  color: #777;
}
</style>
